package com.sms;

import com.sms.exception.SMSException;


import com.sms.exception.DataFileCorruptedException;
import com.sms.manager.MenuManager;
import com.sms.service.*;
import com.sms.service.impl.*;
import com.sms.util.FileUtil;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static com.sms.constants.SystemConstants.*;

/**
 * 学生管理系统主应用程序
 */
public class MainApp {
    private boolean running = false;
    private final StudentService studentService;
    private final CourseService courseService;
    private final ScoreService scoreService;
    private final ReportService reportService;
    private final MenuManager menuManager;

    public MainApp() {
        // 初始化服务层
        this.studentService = new StudentServiceImpl();
        this.courseService = new CourseServiceImpl();
        this.scoreService = new ScoreServiceImpl();
        this.reportService = new ReportServiceImpl();

        // 初始化菜单管理器
        this.menuManager = new MenuManager(studentService, courseService, scoreService, reportService);
    }

    /**
     * 初始化系统
     */
    public void initialize() throws SMSException {
        try {
            // 确保 data 目录存在
            Path dataDir = Path.of("data");
            if (!Files.exists(dataDir)) {
                Files.createDirectories(dataDir);
            }
            // 创建必要的数据文件
            FileUtil.createFileIfNotExists(STUDENT_FILE);
            FileUtil.createFileIfNotExists(COURSE_FILE);
            FileUtil.createFileIfNotExists(SCORE_FILE);
            //System.out.println("当前工作目录: " + System.getProperty("user.dir"));
            System.out.println("系统初始化完成");
        } catch (IOException e) {
            throw new SMSException("系统初始化失败: " + e.getMessage());
        }
    }

    /**
     * 启动应用程序
     */
    public void start() throws SMSException {
        if (running) {
            throw new SMSException("系统已经在运行中");
        }

        try {
            initialize();
            running = true;

            // 进入主菜单循环
            while (running) {
                try {
                    menuManager.showMainMenu();
                    int choice = menuManager.readIntInput();
                    menuManager.handleMainMenuChoice(choice);
                } catch (DataFileCorruptedException e) {
                    System.err.println("数据文件错误: " + e.getMessage());
                } catch (SMSException e) {
                    System.err.println("业务错误: " + e.getMessage());
                }
            }
        } catch (Exception e) {
            throw new SMSException("系统启动失败: " + e.getMessage());
        }
    }

    /**
     * 关闭应用程序
     */
    public void shutdown() throws SMSException {
        if (!running) {
            throw new SMSException("系统未运行");
        }

        try {
            running = false;
            System.out.println("系统正在关闭...");
            System.out.println("感谢使用学生管理系统，再见！");
        } catch (Exception e) {
            throw new SMSException("系统关闭异常: " + e.getMessage());
        }
    }

    /**
     * 获取学生服务实例
     */
    public StudentService getStudentService() {
        return studentService;
    }

    /**
     * 检查系统是否正在运行
     */
    public boolean isRunning() {
        return running;
    }


    /**
     * 应用程序入口
     */
    public static void main(String[] args) {
        MainApp app = new MainApp();

        try {
            app.start();
        } catch (SMSException e) {
            System.err.println("系统错误: " + e.getMessage());
            System.exit(1);
        }
    }
}